using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

public class TransitionManager : MonoBehaviour
{
    public string gameScene;
    [SerializeField] private CanvasGroup canvasGroup;
    [SerializeField] private TextMeshProUGUI text;

    private bool isFade;
    
    public void StartGame()
    {
        StartCoroutine(CortinueStartGame());
    }

    private IEnumerator CortinueStartGame()
    {
        text.text = "正在准备装备";
        yield return Fade(1);
        SceneManager.LoadScene(gameScene);
    }

    public void EndGame()
    {
        Application.Quit();
    }
    

    public IEnumerator Fade(float targetAlpha)
    {
        isFade = true;
        
        canvasGroup.blocksRaycasts = true;

        float speed = Mathf.Abs(canvasGroup.alpha - targetAlpha)/Settings.fadeDuration;
        while (!Mathf.Approximately(canvasGroup.alpha, targetAlpha))
        {
            canvasGroup.alpha = Mathf.MoveTowards(canvasGroup.alpha, targetAlpha, speed * Time.deltaTime);
            Debug.Log("1");
            yield return null;
        }

        canvasGroup.blocksRaycasts = false;
        isFade = false;
    }
}
